﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan
{
    public partial class ClientLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pnHouseList.Visible = false;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string UserName = this.txtUserName.Text;
            string pass = CoreLibrary.CoreSecurity.getMd5Hash(this.txtPassword.Text);

            Model.User u = new Model.User().GetUser(UserName);

            if (u.UserName.ToString().ToLower() == UserName.ToLower())
            {
                if (u.Password == pass && u.UserType == "guest")
                {
                    Session["ClientId"] = u.Id;
                    Session["ClientName"] = u.UserName;
                    Session["ClientType"] = u.UserType;
                    Session["Email"] = u.Email;
                    if (u.HouseCount > 1)
                    {
                        dsHouse.SelectCommand = "select houseid, housename from house where userid=" + u.Id;
                        ddlHouse.DataBind();
                        this.Panel1.Visible = false;
                        if (Request.Browser.IsMobileDevice)
                        {
                            Response.Redirect("devices/");
                        }
                        else
                            this.pnHouseList.Visible = true;
                    }
                    else
                    {
                        dsHouse.SelectCommand = "select houseid, housename from house where userid=" + u.Id;
                        ddlHouse.DataBind();
                        Session["HouseId"] = this.ddlHouse.SelectedValue;
                        Response.Redirect("Default.aspx");
                    }
                   
                }
                else
                {
                    this.loginSms.Text = "Invalid password. Try again!";
                }
            }
            else
            {
                this.loginSms.Text = "Invalid user name! Make sure you type the name correctly.";

                //this.loginSms.Text = u.UserName + "," + UserName;
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            Session["HouseId"] = this.ddlHouse.SelectedValue;
            Response.Redirect("Default.aspx");
        }
    }
}